/**
 * This file is part of Sonedyan.
 * 
 * Sonedyan is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation;
 * either version 3 of the License, or (at your option) any
 * later version.
 *
 * Sonedyan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Octave; see the file COPYING.  If not
 * see <http://www.gnu.org/licenses/>.
 * 
 * Copyright (C) 2009-2013 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>
 */

package org.unige.mpej.eckmann.sonedyan.twitter;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;

/**
 * Twitter index searcher
 */
public class TweetIndexSearcher 
{
	private static Logger log = Logger.getLogger(org.unige.mpej.eckmann.sonedyan.twitter.TweetIndexSearcher.class);
	
	private static final String DIR_PATH = "/home/jimmy/lucene";
	private static final int HITS_PER_PAGE = 10;
	
	private IndexSearcher searcher;
	
	/**
	 * constructor
	 */
	public TweetIndexSearcher()
	{
		try
		{
			Directory index = new SimpleFSDirectory(new File(DIR_PATH));
			IndexReader reader = IndexReader.open(index);
			this.searcher = new IndexSearcher(reader);
		}
		catch(IOException e)
		{
			log.error("An error occured: " + e.getMessage());
		}
	}
	
	/**
	 * search hashtags
	 */
	public ScoreDoc[] searchHashTags(Query q)
	{
		try
		{
			TopScoreDocCollector collector = TopScoreDocCollector.create(HITS_PER_PAGE, true);
			searcher.search(q, collector);
			return collector.topDocs().scoreDocs;
		}
		catch(IOException e)
		{
			log.error("An error occured: " + e.getMessage());
			return null;
		}
	}
	
	/**
	 * main method
	 */
	public static void main(String args[])
	{
		TweetIndexSearcher searcher = new TweetIndexSearcher();
		
		String querystr = args.length > 0 ? args[0] : "lucene";
		//Query q = new QueryParser(Version.LUCENE_41, "title", analyzer).parse(querystr);
	}
	
}
